package com.xxw.yygh.hosp.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.xxw.yygh.hosp.repository.DepartmentRepository;
import com.xxw.yygh.hosp.service.DepartmentService;

import com.xxw.yygh.model.hosp.Department;
import com.xxw.yygh.vo.hosp.DepartmentVo;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentRepository departmentRepository;

    @Override
    public void savaDepartment(Map<String, Object> resultMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(resultMap), Department.class);
        //医院编号+科室编号 联合查询
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department platformDepartment=departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);

        if (platformDepartment == null) {
            //数据不存在，新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            //数据存在，修改
            department.setCreateTime(platformDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(platformDepartment.getIsDeleted());
            department.setId(platformDepartment.getId());

            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> getDepartmentPage(Map<String, Object> resultMap) {
        Integer page = Integer.parseInt((String) resultMap.get("page"));
        Integer limit = Integer.parseInt((String) resultMap.get("limit"));
        String hoscode = (String) resultMap.get("hoscode");

        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);

        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(Map<String, Object> stringObjectMap) {
        String hoscode = (String) stringObjectMap.get("hoscode");
        String depcode = (String) stringObjectMap.get("depcode");

        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> all = departmentRepository.findAll(example);

        //map的key就是大科室  value就是大科室下的子科室列表
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        ArrayList<DepartmentVo> bigDepartmentList = new ArrayList<>();

        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            //大科室编号
            String bigcode = entry.getKey();
            //当前大科室底下所有子科室列表
            List<Department> value = entry.getValue();

            //存放子科室list
            ArrayList<DepartmentVo> childDepartmentVoList = new ArrayList<>();
            //遍历子科室
            for (Department childDepartment : value) {
                DepartmentVo childDepartmentVo = new DepartmentVo();
                //当前子科室编号
                String depcode = childDepartment.getDepcode();
                //当前子科室的名称
                String depname = childDepartment.getDepname();
                childDepartmentVo.setDepcode(depcode);
                childDepartmentVo.setDepname(depname);

                childDepartmentVoList.add(childDepartmentVo);

            }
            bigDepartmentVo.setDepcode(bigcode);
            bigDepartmentVo.setDepname(value.get(0).getBigname());
            bigDepartmentVo.setChildren(childDepartmentVoList);

            bigDepartmentList.add(bigDepartmentVo);

        }

        return bigDepartmentList;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return "";
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);

    }
}
